In [16]:
def count_words(sentence):
"""
Function that takes a string as argument and returns
a dictionary which has words as the keys and the number
of times each word was seen in the sentence as the value.
"""
return {word:sentence.count(word) for word in sentence.lower().split()}
In [21]:
count_words("oh what a day what a lovely day") # wrong: 'a' is counted for the whole sentence
Out[21]:
In [22]:
count_words("don't stop believing")
Out[22]:
In [19]:
count_words("Oh what a day what a lovely day") # wrong: 'oh' does not seem to be counted
Out[19]:
In [23]:
count_words("Oh what a day, what a lovely day!") # wrong: day! and day, are counted as different
Out[23]:
In [24]:
def count_words(sentence):
"""
Function that takes a string as argument and returns
a dictionary which has words as the keys and the number
of times each word was seen in the sentence as the value.
"""
words = sentence.lower().split()
return {word:words.count(word) for word in words}
In [25]:
count_words("oh what a day what a lovely day")
Out[25]:
In [26]:
count_words("don't stop believing")
Out[26]:
In [27]:
count_words("Oh what a day what a lovely day")
Out[27]:
In [28]:
count_words("Oh what a day, what a lovely day!") # wrong: day! and day, are counted as different
Out[28]:
In [30]:
import string
In [31]:
string.punctuation
Out[31]:
In [40]:
def count_words(sentence):
"""
Function that takes a string as argument and returns
a dictionary which has words as the keys and the number
of times each word was seen in the sentence as the value.
"""
words = sentence.lower().split()
words = [word.strip(string.punctuation) for word in words] # Removing punctuations outside of words
return {word:words.count(word) for word in words}
In [41]:
count_words("oh what a day what a lovely day")
Out[41]:
In [42]:
count_words("don't stop believing")
Out[42]:
In [43]:
count_words("Oh what a day what a lovely day")
Out[43]:
In [44]:
count_words("Oh what a day, what a lovely day!")
Out[44]:
In [45]:
import unittest
class CountWordsTests(unittest.TestCase):
"""Tests for count_words."""
def test_simple_sentence(self):
actual = count_words("oh what a day what a lovely day")
expected = {'oh': 1, 'what': 2, 'a': 2, 'day': 2, 'lovely': 1}
self.assertEqual(actual, expected)
def test_apostrophe(self):
actual = count_words("don't stop believing")
expected = {"don't": 1, 'stop': 1, 'believing': 1}
self.assertEqual(actual, expected)
# To test the Bonus part of this exercise, comment out the following line
#@unittest.expectedFailure
def test_capitalization(self):
actual = count_words("Oh what a day what a lovely day")
expected = {'oh': 1, 'what': 2, 'a': 2, 'day': 2, 'lovely': 1}
self.assertEqual(actual, expected)
# To test the Bonus part of this exercise, comment out the following line
#@unittest.expectedFailure
def test_symbols(self):
actual = count_words("Oh what a day, what a lovely day!")
expected = {'oh': 1, 'what': 2, 'a': 2, 'day': 2, 'lovely': 1}
self.assertEqual(actual, expected)
if __name__ == "__main__":
unittest.main(argv=['first-arg-is-ignored'], exit=False)
In [ ]: